package com.muyu.danmo.service.impl;

import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoFontType;
import com.muyu.danmo.mapper.DanmoFontTypeMapper;
import com.muyu.danmo.service.IDanmoFontTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字体类型Service业务层处理
 *
 * @author yangle
 * @date 2024-09-14
 */
@Service
public class DanmoFontTypeServiceImpl implements IDanmoFontTypeService
{
    @Autowired
    private DanmoFontTypeMapper danmoFontTypeMapper;

    /**
     * 查询字体类型
     *
     * @param id 字体类型主键
     * @return 字体类型
     */
    @Override
    public DanmoFontType selectDanmoFontTypeById(Long id)
    {
        return danmoFontTypeMapper.selectDanmoFontTypeById(id);
    }

    /**
     * 查询字体类型列表
     *
     * @param danmoFontType 字体类型
     * @return 字体类型
     */
    @Override
    public List<DanmoFontType> selectDanmoFontTypeList(DanmoFontType danmoFontType)
    {
        return danmoFontTypeMapper.selectDanmoFontTypeList(danmoFontType);
    }

    /**
     * 新增字体类型
     *
     * @param danmoFontType 字体类型
     * @return 结果
     */
    @Override
    public int insertDanmoFontType(DanmoFontType danmoFontType)
    {
        danmoFontType.setCreateTime(DateUtils.getNowDate());
        return danmoFontTypeMapper.insertDanmoFontType(danmoFontType);
    }

    /**
     * 修改字体类型
     *
     * @param danmoFontType 字体类型
     * @return 结果
     */
    @Override
    public int updateDanmoFontType(DanmoFontType danmoFontType)
    {
        danmoFontType.setUpdateTime(DateUtils.getNowDate());
        return danmoFontTypeMapper.updateDanmoFontType(danmoFontType);
    }

    /**
     * 批量删除字体类型
     *
     * @param ids 需要删除的字体类型主键
     * @return 结果
     */
    @Override
    public int deleteDanmoFontTypeByIds(Long[] ids)
    {
        return danmoFontTypeMapper.deleteDanmoFontTypeByIds(ids);
    }

    /**
     * 删除字体类型信息
     *
     * @param id 字体类型主键
     * @return 结果
     */
    @Override
    public int deleteDanmoFontTypeById(Long id)
    {
        return danmoFontTypeMapper.deleteDanmoFontTypeById(id);
    }

    @Override
    public List<String> selectAuthor(DanmoFontType danmoFontType) {
        // 1. 根据 fontType 确定查询的表
        String tableName = determineTable(danmoFontType.getId());
        // 2. 执行分表查询操作，构建查询条件
        List<String > result = fetchFromTable(tableName, danmoFontType);
        return result;
    }
    private String determineTable(Long fontType) {
        // 根据 fontType 确定表名
        if (fontType == 1) {
            return "danmo_font_caoshu";
        }
        if (fontType == 2){
            return "danmo_font_hangshu";
        }

        if (fontType == 3){
            return "danmo_font_lishu";
        }

        if (fontType == 5){
            return "danmo_font_kaishu";

        }

        if (fontType == 6){
            return "danmo_font_xiaokai";
        }

        if (fontType == 22){
            return "danmo_font_weibei";
        }

        if (fontType == 23){
            return "danmo_font_hangcao";
        }

        if (fontType == 24){
            return "danmo_font_zhangcao";
        }

        if (fontType == 25){
            return "danmo_font_zhuanshu";
        }
        return "danmo_font_generate";
    }
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private List<String> fetchFromTable(String tableName, DanmoFontType danmoFontType) {
        // 构建查询条件，获取所有作者
        StringBuilder query = new StringBuilder(
                "SELECT g.author " +
                        "FROM " + tableName + " g " +
                        "WHERE g.font_type_id = " + danmoFontType.getId());

        List<String> params = new ArrayList<>();

        // 执行查询并返回结果
        return jdbcTemplate.query(query.toString(), params.toArray(), new RowMapper<String>() {
                    @Override
                    public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return rs.getString("author");
                    }
                }).stream()
                .distinct()  // 使用 Java 8 的流处理去重
                .collect(Collectors.toList());
    }

}
